

(function(jQuery){

	var images = [];
	
	jQuery.fn.rolloverImages =function(options){
		
		var o = jQuery.extend({}, jQuery.fn.rolloverImages.defaults, options);
		
		return this.each(function(){
			
			var $node = $(this);
			
			$node.find(o.target)
				.andSelf()
					.filter(o.target)
						.not(o.exclude)
							.each(function(){
				
				var image ={};
				var $node = $(this);
				var src = $node.attr('src');
				var filetype = src.substring(src.lastIndexOf('.'));
				var basename = src.substring(0, src.length-filetype.length);
				
				for (var type in o.eventHandlers) {
					var src = basename + o.eventHandlers[type] + filetype;
					image[type] = preloadImage(src);
				}
				
				images.push(image);
				
				for (var type in o.eventHandlers) {
					$node.bind(type, function(e){
						var type = e.type;
						var src = image[type].src;
						$node.attr({'src': src});
					});
				}
			});
			
			return this;
		});
	}
	
	jQuery.fn.rolloverImages.defaults = {
		'target': 'img, input:image',
		'exclude': '.no-rollover',
		'eventHandlers': {
			'mouseover': '_on',
			'mouseout': ''
		}
	};
	
	function preloadImage(src) {
		var img = new Image();
		img.src = src;
		return img;
	}
	
})(jQuery);



